package com.qf.hospital.controller;

import com.github.pagehelper.PageInfo;
import com.qf.hospital.DTO.AddUserDTO;
import com.qf.hospital.DTO.SysUserDTO;
import com.qf.hospital.VO.R;
import com.qf.hospital.VO.SysUserRoleVO;
import com.qf.hospital.VO.SysUserVO;
import com.qf.hospital.entity.SysUser;
import com.qf.hospital.enums.HospitalExceptionEnum;
import com.qf.hospital.service.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 何星宇
 * @version V1.0
 * @Date 2021/10/18 9:55
 */

@RestController
public class UserController {
    private Logger log = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private SysUserService userService;

    /**
     * 查询用户
     * @param sysUserDTO
     * @return
     */
    @GetMapping("/user/selectAllUser")
    public R selectAllUser(SysUserDTO sysUserDTO){
        //调用service查询用户
        PageInfo<SysUser> pageInfo =  userService.findAllUser(
                sysUserDTO.getRealname(),
                sysUserDTO.getLoginname(),
                sysUserDTO.getAddress(),
                sysUserDTO.getPhone(),
                sysUserDTO.getIdentity(),
                sysUserDTO.getPage(),
                sysUserDTO.getLimit()
        );
        //获取数据数量
        long count = pageInfo.getTotal();
        //获取data
        List<SysUserVO> data = new ArrayList<>();
        List<SysUser> userList = pageInfo.getList();
        for (SysUser sysUser : userList) {
            SysUserVO userVO = new SysUserVO();
            BeanUtils.copyProperties(sysUser, userVO);
            userVO.setPwd("");
            data.add(userVO);
        }
        return R.ok(data, count);
    }

    /**
     * 添加用户
     * @param userDTO
     * @param bindingResult
     * @return
     */
    @PostMapping("/user/addUser")
    public R addUser(@Valid AddUserDTO userDTO, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            //记录日志
            log.error("【添加用户】 参数不合法");
            return R.error(HospitalExceptionEnum.INVALID_PARAMETER, bindingResult.getFieldError().getDefaultMessage());
        }
        userService.addUser(userDTO);
        return R.ok("添加用户成功");
    }

    /**
     * 分配角色信息
     * @param userId
     * @return
     */
    @GetMapping("/user/initUserRole")
    public R initUserRole(@RequestParam(value = "userid") Integer userId){
        //校验
        if (userId==null){
            //记录日志
            log.error("【分配角色信息】 参数不合法");
            return R.error(HospitalExceptionEnum.INVALID_PARAMETER);
        }
        //调用service获取角色信息
        List<SysUserRoleVO> data =  userService.findUserRoleByUserId(userId);
        return R.ok(data);
    }


    /**
     * 保存用户角色信息
     * @param userId
     * @param ids
     * @return
     */
    @PostMapping("/user/saveUserRole")
    public R saveUserRole(@RequestParam(value = "userid") Integer userId,Integer[] ids){
        //校验
        if (userId==null){
            //记录日志
            log.error("【分配角色信息】 参数不合法");
            return R.error(HospitalExceptionEnum.INVALID_PARAMETER);
        }
        userService.saveUserRole(userId,ids);
        return R.ok("保存成功");
    }
}
